package com.mtiiot.fstcpserver.fastdfs.core;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;

public class IniFileReader {
	private Hashtable paramTable;
	private String conf_filename;

	public IniFileReader(String conf_filename) throws FileNotFoundException,
			IOException {
		this.conf_filename = conf_filename;
		loadFromFile(conf_filename);
	}

	public IniFileReader(Properties p) throws Exception {
		loadFromFile(p);
	}

	public String getConfFilename() {
		return this.conf_filename;
	}

	public String getStrValue(String name) {
		Object obj = this.paramTable.get(name);
		if (obj == null) {
			return null;
		}

		if ((obj instanceof String)) {
			return (String) obj;
		}

		return (String) ((ArrayList) obj).get(0);
	}

	public int getIntValue(String name, int default_value) {
		String szValue = getStrValue(name);
		if (szValue == null) {
			return default_value;
		}

		return Integer.parseInt(szValue);
	}

	public boolean getBoolValue(String name, boolean default_value) {
		String szValue = getStrValue(name);
		if (szValue == null) {
			return default_value;
		}

		return (szValue.equalsIgnoreCase("yes"))
				|| (szValue.equalsIgnoreCase("on"))
				|| (szValue.equalsIgnoreCase("true")) || (szValue.equals("1"));
	}

	public String[] getValues(String name) {
		Object obj = this.paramTable.get(name);
		if (obj == null) {
			return null;
		}

		if ((obj instanceof String)) {
			String[] values = new String[1];
			values[0] = ((String) obj);
			return values;
		}

		Object[] objs = ((ArrayList) obj).toArray();
		String[] values = new String[objs.length];
		System.arraycopy(objs, 0, values, 0, objs.length);
		return values;
	}

	private void loadFromFile(String conf_filename)
			throws FileNotFoundException, IOException {
		FileReader fReader = new FileReader(conf_filename);
		BufferedReader buffReader = new BufferedReader(fReader);
		this.paramTable = new Hashtable();
		try {
			String line;
			while ((line = buffReader.readLine()) != null) {
				line = line.trim();
				if ((line.length() != 0) && (line.charAt(0) != '#')) {
					String[] parts = line.split("=", 2);
					if (parts.length == 2) {
						String name = parts[0].trim();
						String value = parts[1].trim();

						Object obj = this.paramTable.get(name);
						if (obj == null) {
							this.paramTable.put(name, value);
						} else if ((obj instanceof String)) {
							ArrayList valueList = new ArrayList();
							valueList.add(obj);
							valueList.add(value);
							this.paramTable.put(name, valueList);
						} else {
							ArrayList valueList = (ArrayList) obj;
							valueList.add(value);
						}
					}
				}
			}
		} finally {
			fReader.close();
		}
		String line;
	}

	private void loadFromFile(Properties p) throws Exception {
		try {
			String connect_timeout = p.getProperty("connect_timeout");
			String network_timeout = p.getProperty("network_timeout");
			String charset = p.getProperty("charset");
			String httptracker_http_port = p
					.getProperty("http.tracker_http_port");
			String httpanti_steal_token = p
					.getProperty("http.anti_steal_token");
			String httpsecret_key = p.getProperty("http.secret_key");
			String tracker_server = p.getProperty("tracker_server");
			this.paramTable = new Hashtable();
			this.paramTable.put("connect_timeout", connect_timeout);
			this.paramTable.put("network_timeout", network_timeout);
			this.paramTable.put("charset", charset);
			this.paramTable
					.put("http.tracker_http_port", httptracker_http_port);
			this.paramTable.put("http.anti_steal_token", httpanti_steal_token);
			this.paramTable.put("http.secret_key", httpsecret_key);
			String[] tss = tracker_server.split(",");
			if (tss.length == 1) {
				this.paramTable.put("tracker_server", tracker_server);
			} else {
				List valueList = new ArrayList();
				for (String ts : tss) {
					valueList.add(ts);
				}
				this.paramTable.put("tracker_server", valueList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
}
